#include "node_edge.h"




NodeEdge::NodeEdge()
{

}

NodeEdge::NodeEdge(NodeScene *start_node,NodeScene *end_node, NodeSocket *start_socket, NodeSocket *end_socket, QGraphicsItem *parent)
    //:QGraphicsPathItem(parent)
//  为什么会偏离  加上了父级的位置？
{
    this->start_socket = start_socket;
    this->end_socket = end_socket;
    this->start_node = start_node;
    this->end_node = end_node;
    this->start_socket_pos = start_socket->pos();
    this->end_socket_pos = end_socket->pos();
    this->start_pos = this->start_node->item->pos() + start_socket_pos;
    this->end_pos = this->end_node->item->pos() + end_socket_pos;

    path = new QPainterPath(start_pos);

    this->setFlag(QGraphicsItem::ItemIsSelectable);
}

NodeEdge::NodeEdge(NodeSocket *start_socket, NodeSocket *end_socket, QGraphicsItem *parent):QGraphicsPathItem(parent)
{
    this->start_socket = start_socket;
    this->end_socket = end_socket;
    SetStartPos(start_socket);
    SetEndPos(end_socket);

    path = new QPainterPath(start_pos);
    qDebug()<<"start_socket"<<start_socket->index<<start_socket->pos();
    qDebug()<<"end_socket"<<end_socket->index<<end_socket->pos();


    this->setFlag(QGraphicsItem::ItemIsSelectable);
    //this->setFlag(QGraphicsItem::ItemIsMovable);
}

NodeEdge::NodeEdge(const NodeEdge &nodeEdge):QGraphicsPathItem()
{
    Q_UNUSED(nodeEdge);

}

void NodeEdge::operator=(const NodeEdge &nodeEdge)
{
    Q_UNUSED(nodeEdge);
}

NodeEdge::~NodeEdge()
{
    qDebug()<<"delete edge";
//    delete start_socket;
//    delete end_socket;
    delete path;
    start_socket = NULL;
    end_socket = NULL;
    start_node = NULL;
    end_node = NULL;
    path = NULL;
}


void NodeEdge::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);
    QPen pen1(QColor("#aaaaaa"));
    pen1.setWidth(2);
    QPen pen2(QColor("#FF0000"));
    pen2.setWidth(2);
    this->start_pos = this->start_node->item->pos() + start_socket_pos;
    this->end_pos = this->end_node->item->pos() + end_socket_pos;
    path = new QPainterPath(start_pos);


    // Direct / Bezier
    painter->setRenderHint(QPainter::Antialiasing);
    if(!this->isSelected()){
        //painter->setPen(QPen(QColor("#aaaaaa")));
        painter->setPen(pen1);
    }
    else{
        //painter->setPen(QPen(QColor(Qt::red)));
        painter->setPen(pen2);
    }
    painter->setBrush(Qt::NoBrush);

//TODO when simplified and when *pen

    qreal distance = (end_pos.x() - start_pos.x()) * 0.5;
    if(start_pos.x() > end_pos.y()&&start_pos.y() > end_pos.x()){
        distance *= -1;
    }

    path->moveTo(start_pos);
    path->cubicTo(start_pos.x() + distance,
                  start_pos.y(),
                  end_pos.x() - distance,
                  end_pos.y(),
                  end_pos.x(),
                  end_pos.y());


    //path->lineTo(end_pos);
    this->setPath(*path);
    painter->drawPath(*path);

}

QRectF NodeEdge::boundingRect() const
{
    return this->path->boundingRect();
    //return this->shape().boundingRect();

}

void NodeEdge::SetIndex(int index)
{
    this->index = index;
}

void NodeEdge::SetStartPos(NodeSocket *start_socket)
{
    this->start_pos = start_socket->pos();
}

void NodeEdge::SetEndPos(NodeSocket *end_socket)
{
    this->end_pos = end_socket->pos();
}
